using Microsoft.SPOT;

namespace CodeConsult.Nordic.nRF8001.ApplicationControlInterface.Events
{
    public static class AciEventFactory
    {
        private const int OpCodeOffset = 0;

        public static AciEventBase Create(AciExchangeMessage exchangeMessage, bool supressDebugWhenReceivedMessageIsEmpty = true)
        {
            if (exchangeMessage.ReceivedMessage.Length <= OpCodeOffset)
            {
                if (!supressDebugWhenReceivedMessageIsEmpty)
                    Debug.Print("Could not create ACI event from message: message to small (" + exchangeMessage.ReceivedMessage.Length + " bytes)");
                return null;
            }

            var opCode = exchangeMessage.ReceivedMessage[OpCodeOffset];

            AciEventBase res = null;
            switch (opCode)
            {
                case CommandResponseEvents.CommandResponseEvent.OpCode:
                    res = new CommandResponseEvents.CommandResponseEvent();
                    break;
                case DataEvents.DataAckEvent.OpCode:
                    res = new DataEvents.DataAckEvent();
                    break;
                case DataEvents.DataCreditEvent.OpCode:
                    res = new DataEvents.DataCreditEvent();
                    break;
                case DataEvents.DataReceivedEvent.OpCode:
                    res = new DataEvents.DataReceivedEvent();
                    break;
                case DataEvents.PipeErrorEvent.OpCode:
                    res = new DataEvents.PipeErrorEvent();
                    break;
                case SystemEvents.BondStatusEvent.OpCode:
                    res = new SystemEvents.BondStatusEvent();
                    break;
                case SystemEvents.ConnectedEvent.OpCode:
                    res = new SystemEvents.ConnectedEvent();
                    break;
                case SystemEvents.StartedEvent.OpCode:
                    res = new SystemEvents.StartedEvent();
                    break;
                case SystemEvents.DisconnectedEvent.OpCode:
                    res = new SystemEvents.DisconnectedEvent();
                    break;
                case SystemEvents.DisplayKeyEvent.OpCode:
                    res = new SystemEvents.DisplayKeyEvent();
                    break;
                case SystemEvents.EchoEvent.OpCode:
                    res = new SystemEvents.EchoEvent();
                    break;
                case SystemEvents.HardwareErrorEvent.OpCode:
                    res = new SystemEvents.HardwareErrorEvent();
                    break;
                case SystemEvents.KeyRequestEvent.OpCode:
                    res = new SystemEvents.KeyRequestEvent();
                    break;
                case SystemEvents.PipeStatusEvent.OpCode:
                    res = new SystemEvents.PipeStatusEvent();
                    break;
                case SystemEvents.TimingEvent.OpCode:
                    res = new SystemEvents.TimingEvent();
                    break;
                default:
                    Debug.Print("Could not create ACI event from message: unknown opcode (" + opCode +")");
                    break;
            }

            res?.Decode(exchangeMessage);
            return res;
        }
    }
}
